﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Token.Util
{
    public class ExifGetter
    {
        private string manufacturers, model, photoDate, exposureTime, iso, photoInfo, focalLength, apertureValue = "";

        private ExifGetter() { }

        public ExifGetter(Image img)
        {
            FindExifinfo(img);
        }

        public void FindExifinfo(Image img)
        {
            PropertyItem[] pt = img.PropertyItems;
            for (int i = 0; i < pt.Length; i++)
            {
                PropertyItem p = pt[i];
                switch (pt[i].Id)
                {  // 设备制造商 
                    case 0x010F:
                        this.manufacturers = System.Text.ASCIIEncoding.ASCII.GetString(pt[i].Value);
                        break;
                    case 0x0110: // 设备型号 
                        this.model = GetValueOfType2(p.Value);
                        break;
                    case 0x0132: // 拍照时间 
                        this.photoDate = GetValueOfType2(p.Value);
                        break;
                    case 0x829A: // .曝光时间 
                        this.exposureTime = GetValueOfType5(p.Value) + " sec";
                        break;
                    case 0x8827: // ISO 
                        this.Iso = GetValueOfType3(p.Value);
                        break;
                    case 0x010E: // 图像说明
                        this.photoInfo = GetValueOfType2(p.Value);
                        break;
                    case 0x920a: //相片的焦距
                        this.focalLength = GetValueOfType5A(p.Value) + " mm";
                        break;
                    case 0x829D: //相片的光圈值
                        this.apertureValue = GetValueOfType5A(p.Value);
                        break;
                    default:
                        break;
                }
            }
        }

        public string GetValueOfType2(byte[] b)// 对type=2 的value值进行读取
        {
            return System.Text.Encoding.ASCII.GetString(b);
        }
        private static string GetValueOfType3(byte[] b) //对type=3 的value值进行读取
        {
            if (b.Length != 2) return "";
            return Convert.ToUInt16(b[1] << 8 | b[0]).ToString();
        }
        private static string GetValueOfType5(byte[] b) //对type=5 的value值进行读取
        {
            if (b.Length != 8) return "";
            UInt32 fm, fz;
            fm = 0;
            fz = 0;
            fz = Convert.ToUInt32(b[7] << 24 | b[6] << 16 | b[5] << 8 | b[4]);
            fm = Convert.ToUInt32(b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0]);
            return fm.ToString() + "/" + fz.ToString();
        }
        private static string GetValueOfType5A(byte[] b)//获取光圈的值
        {
            if (b.Length != 8) return "";
            UInt32 fm, fz;
            fm = 0;
            fz = 0;
            fz = Convert.ToUInt32(b[7] << 24 | b[6] << 16 | b[5] << 8 | b[4]);
            fm = Convert.ToUInt32(b[3] << 24 | b[2] << 16 | b[1] << 8 | b[0]);
            double temp = (double)fm / fz;
            return (temp).ToString();
        }

        public string Manufacturers       //制造商
        {
            get { return manufacturers; }
            set { manufacturers = value; }
        }

        public string Model        //型号
        {
            get { return model; }
            set { model = value; }
        }

        public string PhotoDate      //拍照时间
        {
            get { return photoDate; }
            set { photoDate = value; }
        }

        public string ExposureTime    //曝光时间
        {
            get { return exposureTime; }
            set { exposureTime = value; }
        }

        public string Iso     //ISO
        {
            get { return iso; }
            set { iso = value; }
        }

        public string PhotoInfo    //图像说明
        {
            get { return photoInfo; }
            set { photoInfo = value; }
        }

        public string FocalLength    //焦距
        {
            get { return focalLength; }
            set { focalLength = value; }
        }

        public string ApertureValue    //光圈值
        {
            get { return apertureValue; }
            set { apertureValue = value; }
        }


    }
}
